package xin.printk.android.data.db.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.util.ArrayList;
import java.util.List;

import xin.printk.android.data.db.DatabaseHelper;
import xin.printk.android.data.db.SQL;
import xin.printk.android.data.entity.Plan;
import xin.printk.android.utils.LogUtils;

public class PlanDao {

    public static List<Plan> getUndonePlans(Context context, int index) {
        return getPlanList(context, SQL.queryUndonePlans, index);
    }

    public static List<Plan> getCompletedPlans(Context context, int index) {
        return getPlanList(context, SQL.queryCompletedPlans, index);
    }

    public static List<Plan> getPlanList(Context context, String sql, int index) {
        List<Plan> list = null;
        SQLiteOpenHelper helper = new DatabaseHelper(context);
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor cursor = db.rawQuery(sql + String.valueOf(index), null);
        if (cursor != null) {
            list = new ArrayList<>();
            while (cursor.moveToNext()) {
                int id = cursor.getInt(cursor.getColumnIndex("id"));
                int userId = cursor.getInt(cursor.getColumnIndex("user_id"));
                int status = cursor.getInt(cursor.getColumnIndex("status"));
                int level = cursor.getInt(cursor.getColumnIndex("level"));
                String title = cursor.getString(cursor.getColumnIndex("title"));
                String content = cursor.getString(cursor.getColumnIndex("content"));
                long completeTime = cursor.getLong(cursor.getColumnIndex("completed_time"));
                long planTime = cursor.getLong(cursor.getColumnIndex("plan_time"));
                long createTime = cursor.getLong(cursor.getColumnIndex("create_time"));
                float rating = cursor.getFloat(cursor.getColumnIndex("rating"));
                String comment = cursor.getString(cursor.getColumnIndex("comment"));
                Plan plan = new Plan();
                plan.setId(id);
                plan.setUserId(userId);
                plan.setStatus(status);
                plan.setLevel(level);
                plan.setTitle(title);
                plan.setContent(content);
                plan.setCompleteTime(completeTime);
                plan.setPlanTime(planTime);
                plan.setCreateTime(createTime);
                plan.setRating(rating);
                plan.setComment(comment);
                list.add(plan);
            }
            cursor.close();
        }
        db.close();
        return list;
    }

    public static long addPlan(Context context, Plan plan) {
        SQLiteOpenHelper helper = new DatabaseHelper(context);
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("user_id", plan.getUserId());
        values.put("status", plan.getStatus());
        values.put("level", plan.getLevel());
        values.put("title", plan.getTitle());
        values.put("content", plan.getContent());
        values.put("completed_time", plan.getCompleteTime());
        values.put("plan_time", plan.getPlanTime());
        values.put("create_time", plan.getCreateTime());
        values.put("rating", plan.getRating());
        values.put("comment", plan.getComment());
        long ret = db.insert(SQL.TABLE_PLAN, null, values);
        db.close();
        return ret;
    }

    public static int updatePlan(Context context, Plan plan) {
        SQLiteOpenHelper helper = new DatabaseHelper(context);
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("user_id", plan.getUserId());
        values.put("status", plan.getStatus());
        values.put("level", plan.getLevel());
        values.put("title", plan.getTitle());
        values.put("content", plan.getContent());
        values.put("completed_time", plan.getCompleteTime());
        values.put("plan_time", plan.getPlanTime());
        values.put("create_time", plan.getCreateTime());
        values.put("rating", plan.getRating());
        values.put("comment", plan.getComment());
        int ret = db.update(SQL.TABLE_PLAN, values, "id = ?", new String[]{String.valueOf(plan.getId())});
        db.close();
        return ret;
    }

    public static int deletePlan(Context context, Plan plan) {
        SQLiteOpenHelper helper = new DatabaseHelper(context);
        SQLiteDatabase db = helper.getWritableDatabase();
        int ret = db.delete(SQL.TABLE_PLAN, "id = ?", new String[]{String.valueOf(plan.getId())});
        db.close();
        return ret;
    }

    public static Plan getPlan(Context context, int planId) {
        SQLiteOpenHelper helper = new DatabaseHelper(context);
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor cursor = db.rawQuery(SQL.queryPlan, new String[]{String.valueOf(planId)});
        if (cursor != null) {
            Plan plan = null;
            if (cursor.moveToFirst()) {
                int id = cursor.getInt(cursor.getColumnIndex("id"));
                int userId = cursor.getInt(cursor.getColumnIndex("user_id"));
                int status = cursor.getInt(cursor.getColumnIndex("status"));
                int level = cursor.getInt(cursor.getColumnIndex("level"));
                String title = cursor.getString(cursor.getColumnIndex("title"));
                String content = cursor.getString(cursor.getColumnIndex("content"));
                long completeTime = cursor.getLong(cursor.getColumnIndex("completed_time"));
                long planTime = cursor.getLong(cursor.getColumnIndex("plan_time"));
                long createTime = cursor.getLong(cursor.getColumnIndex("create_time"));
                float rating = cursor.getFloat(cursor.getColumnIndex("rating"));
                String comment = cursor.getString(cursor.getColumnIndex("comment"));
                plan = new Plan();
                plan.setId(id);
                plan.setUserId(userId);
                plan.setStatus(status);
                plan.setLevel(level);
                plan.setTitle(title);
                plan.setContent(content);
                plan.setCompleteTime(completeTime);
                plan.setPlanTime(planTime);
                plan.setCreateTime(createTime);
                plan.setRating(rating);
                plan.setComment(comment);
            }
            cursor.close();
            db.close();
            return plan;
        }
        return null;
    }
}
